package com.example.lib5.binaryTree;

/**
 * 创建日期：2022/5/26 0026 16:53
 *
 * @author tony.sun
 * 类说明：
 */
public class BinaryTreeDemo {
    public static void main(String[] args) {
        HeroNode root = new HeroNode(1, "唐三");
        HeroNode ts2 = new HeroNode(2, "吴用");
        HeroNode ts3 = new HeroNode(3, "卢俊");
        HeroNode ts4 = new HeroNode(4, "林聪");
        HeroNode ts5 = new HeroNode(5, "关胜");
        root.setLeft(ts2);
        root.setRight(ts3);
        ts3.setRight(ts4);
        ts3.setLeft(ts5);
        BinaryTree binaryTree = new BinaryTree();
        binaryTree.setRoot(root);
        System.out.println("前序打印");
        binaryTree.preOrder();//12354
        System.out.println("中序");
        binaryTree.infixOrder();//21534
        System.out.println("后续");
        binaryTree.postOrder();//25431
        System.out.println("--------------------------------------查找--------------------------------------");
        HeroNode preNode = binaryTree.preSearch(5);
        if (preNode!=null){
            System.out.println(preNode);
        }else {
            System.out.println("pre为空");
        }
        HeroNode infixNode = binaryTree.infixSearch(5);
        if (infixNode!=null){
            System.out.println(infixNode);
        }else {
            System.out.println("infix为空");
        }
        HeroNode postNode = binaryTree.postSearch(5);
        if (postNode!=null){
            System.out.println(postNode);
        }else {
            System.out.println("pre为空");
        }

        System.out.println("--------------------------------------删除--------------------------------------");
        binaryTree.preOrder();//12354
        binaryTree.delete(3);
        System.out.println("--------------删除后--------------");
        binaryTree.preOrder();//1234

    }
}
//节点
class HeroNode{
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
    //前序遍历
    public void preOrder(){
        System.out.println(this);
        if (this.left!=null){
            this.left.preOrder();
        }
        if (this.right!=null){
            this.right.preOrder();
        }

    }
    //中序遍历
    public void infixOrder(){
        if (this.left!=null){
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right!=null){
            this.right.infixOrder();
        }
    }
    //后序遍历
    public void postOrder(){

        if (this.left!=null){
            this.left.postOrder();
        }
        if (this.right!=null){
            this.right.postOrder();
        }
        System.out.println(this);
    }
    //前序遍历
    public HeroNode preOrderSearch(int findNo){
        System.out.println("查找了~~");
        if (findNo==this.no){
            return this;
        }
        HeroNode resNode=null;
        //左
        if (this.left!=null){
            resNode= this.left.preOrderSearch(findNo);
        }
        if (resNode!=null){
            return resNode;
        }
        //右
        if (this.right!=null){
            resNode=this.right.preOrderSearch(findNo);
        }

        return resNode;
    }
    int time=0;
    //中序遍历
    public HeroNode infixOrderSearch(int findNo){
        time++;

        HeroNode resNode=null;
        //左
        if (this.left!=null){
            resNode= this.left.infixOrderSearch(findNo);
        }
        if (resNode!=null){
            return resNode;
        }
        System.out.println("查找了~~"+time);
        if (findNo==this.no){
            return this;
        }
        //右
        if (this.right!=null){
            resNode=this.right.infixOrderSearch(findNo);
        }

        return resNode;
    }
    //后序遍历
    public HeroNode postOrderSearch(int findNo){
        HeroNode resNode=null;
        //左
        if (this.left!=null){
            resNode= this.left.postOrderSearch(findNo);
        }
        if (resNode!=null){
            return resNode;
        }
        //右
        if (this.right!=null){
            resNode=this.right.postOrderSearch(findNo);
        }
        if (resNode!=null){
            return resNode;
        }
        System.out.println("查找了~~");
        if (findNo==this.no){
            return this;
        }

        return resNode;
    }


    public void delete(int no) {
        if (this.left!=null&&this.left.no==no){
            this.left=null;
            return;
        }
        if (this.right!=null&&this.right.no==no){
            this.right=null;
            return;
        }
        if (this.left!=null){
            this.left.delete(no);
        }
        if (this.right!=null){
            this.right.delete(no);
        }
    }
}
//二叉树
class BinaryTree{
    private HeroNode root;

    public void setRoot(HeroNode root) {
        this.root = root;
    }

    public void preOrder(){
        if (root!=null) {
            root.preOrder();
        }else {
            System.out.println("二叉树为空");
        }
    }
    public void infixOrder(){
        if (root!=null) {
            root.infixOrder();
        }else {
            System.out.println("二叉树为空");
        }
    }
    public void postOrder(){
        if (root!=null) {
            root.postOrder();
        }else {
            System.out.println("二叉树为空");
        }
    }
    //前中后序查找
    public HeroNode preSearch(int no){
        if (root!=null){
            return root.preOrderSearch(no);
        }else {
            return null;
        }
    }
    public HeroNode infixSearch(int no){
        if (root!=null){
            return root.infixOrderSearch(no);
        }else {
            return null;
        }
    }
    public HeroNode postSearch(int no){
        if (root!=null){
            return root.postOrderSearch(no);
        }else {
            return null;
        }
    }

    public void delete(int no) {
        if (root!=null) {
            if (root.getNo()==no) {
                root=null;
            }else {
                root.delete(no);
            }
        }else {
            System.out.println("节点为空，无法删除");


        }

    }
}
